﻿using Bilibili.Model.Resource;
using Bilibili.Model.Video;
using Bilibili.WebClient.Common;
using Newtonsoft.Json.Linq;
using PCLUntils.Json;
using Proto.Reply;
using Proto.Request;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class PlayerClient : ClientBase
    {
        public async Task<ReturnModel<VideoInfoModels>> GetVideoInfo(string avId)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://app.bilibili.com/x/v2/view", $"&aid={avId}&plat=0", false);
                string results = await ApiClient.GetResults(apiInfo);
                VideoInfoModels model = results.ParseObject<VideoInfoModels>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<VideoInfoModels>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> GetBiliPlus(string avId)
        {
            try
            {
                string re2 = await ApiClient.GetResults($"https://www.biliplus.com/api/view?id={avId}&access_key={ApiProvider.Instance.AccessKey}");
                var obj = re2.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<PlayViewReply>> VideoPlayView(long aid, long cid, int qn, int fnval, CodeType codeType)
        {
            var message = new PlayViewReq
            {
                EpId = aid,
                Cid = cid,
                Qn = qn,
                Fnval = fnval,
                Fourk = 1,
                PreferCodecType = codeType
            };
            var result = await GrpcRequest.Instance.SendMessage("https://app.bilibili.com/bilibili.app.playurl.v1.PlayURL/PlayView", message, ApiProvider.Instance.AccessKey);
            if (result.Status)
            {
                var m = PlayViewReply.Parser.ParseFrom(result.Results);
                return BuildSuccessResult(m);
            }
            else
                return BuildFailedResult<PlayViewReply>(result.Message);
        }
        public async Task<ReturnModel<PlayViewReply>> BangumiPlayView(long epid, long cid, int qn, int fnval, CodeType codeType)
        {
            var message = new PlayViewReq
            {
                EpId = epid,
                Cid = cid,
                Qn = qn,
                Fnval = fnval,
                Fourk = 1,
                PreferCodecType = codeType
            };
            var result = await GrpcRequest.Instance.SendMessage("https://app.bilibili.com/bilibili.pgc.gateway.player.v1.PlayURL/PlayView", message, ApiProvider.Instance.AccessKey);
            if (result.Status)
            {
                var m = PlayViewReply.Parser.ParseFrom(result.Results);
                return BuildSuccessResult(m);
            }
            else
                return BuildFailedResult<PlayViewReply>(result.Message);
        }
    }
}